import { getLoadMapList, getSelectDataMapList, getTenantID, requestFunComponFiex } from '@/utils';
import { useRequest } from 'umi';
import { ILoadMapTypeAll } from '@/common';
import { adminAreadDataGet, loadMdicMap } from '@/services/BaseLayout';
import { useCallback, useMemo, useState } from 'react';
import { AssetDisposeManageAPI } from '../common/type';
import { ManageSearchColumns } from '../mconfig';
import { disposeArea } from '../common';
import { getRequestIp } from '../../../Assetmanagement/Acommon';
import moment from 'moment';

const MaterialManageServices = (curRes: Record<string, any>) => {
  /***获取数据字典**/
  const { loadMapInfo, mapLoading } = getLoadMapList([
    `${getTenantID()}${ILoadMapTypeAll.ASSET_TYPE}`, //资产类型
  ]);
  /** 资产类型 */
  const assetType = getSelectDataMapList(loadMapInfo?.[`${getTenantID()}${ILoadMapTypeAll.ASSET_TYPE}`], 'name', 'id', true);
  // const { loadMapInfo: loadMdicMapList } = getLoadMapList(loadMdicIds);

  /**获取省市区数据 */
  const areaList = useRequest(() => {
    return adminAreadDataGet({});
  });

  /**行政区域数据 */
  let areaListData = useMemo(() => {
    if (areaList?.data?.msgFlag) {
      return areaList?.data?.obj;
    } else {
      return [];
    }
  }, [areaList?.data]);

  /***保存当前的搜索参数**/
  const [manageTableParams, setManageTableParams] = useState<Record<string, any>>({});

  /***管理页获取数据方法**/
  const { tableProps, data, loading, run } = useRequest(
    (reqParams: AssetDisposeManageAPI.IAssetTableRequestType) => {
      let PostParams = {
        current: 1,
        pageSize: 10,
        sort: 'createTime',
        order: 'desc',
        ...reqParams,
      };

      const reqbody = PostParams;

      let reqUrl = '/assetApply/dataGrid/ASSET_APPLY_UES';

      /***发起请求数据接口**/
      return requestFunComponFiex('POST', reqUrl, reqbody);
    },
    {
      manual: true,
      paginated: true,
      defaultPageSize: 10,
    },
  );

  /***重新请求的方法**/
  const reloadRequest = useCallback((reqParams: AssetDisposeManageAPI.IAssetTableRequestType | any) => {
    if (reqParams?.statisticalTime) {
      const [statisticalTimeStart, statisticalTimeEnd] = reqParams.statisticalTime;
      if (reqParams.timeType === 1) {
        reqParams['statisticalTimeStart'] = moment(statisticalTimeStart).startOf('year').format('YYYY-MM-DD HH:mm:ss');
        reqParams['statisticalTimeEnd'] = moment(statisticalTimeEnd).endOf('year').format('YYYY-MM-DD HH:mm:ss');
      } else if (reqParams.timeType === 2) {
        reqParams['statisticalTimeStart'] = moment(statisticalTimeStart).startOf('month').format('YYYY-MM-DD HH:mm:ss');
        reqParams['statisticalTimeEnd'] = moment(statisticalTimeEnd).endOf('month').format('YYYY-MM-DD HH:mm:ss');
      } else if (reqParams.timeType === 3) {
        reqParams['statisticalTimeStart'] = moment(statisticalTimeStart).startOf('day').format('YYYY-MM-DD HH:mm:ss');
        reqParams['statisticalTimeEnd'] = moment(statisticalTimeEnd).endOf('day').format('YYYY-MM-DD HH:mm:ss');
      }
      delete reqParams['statisticalTime'];
      delete reqParams['statisticalTimeDept'];
      delete reqParams['statisticalTimeUser'];
    }

    /**处理创建时间*/
    if (reqParams?.createTime) {
      const [timeStart, timeEnd] = reqParams.createTime;
      reqParams['createTimeStart'] = timeStart.format('YYYY-MM-DD 00:00:00');
      reqParams['createTimeEnd'] = timeEnd.format('YYYY-MM-DD 23:59:59');
      delete reqParams['createTime'];
    }

    /**处理签发时间*/
    if (reqParams?.signTime) {
      const [timeStart, timeEnd] = reqParams.signTime;
      reqParams['signTimeStart'] = timeStart.format('YYYY-MM-DD 00:00:00');
      reqParams['signTimeEnd'] = timeEnd.format('YYYY-MM-DD 23:59:59');
      delete reqParams['signTime'];
    }

    /**处理修改时间*/
    if (reqParams?.modifyTime) {
      const [timeStart, timeEnd] = reqParams.modifyTime;
      reqParams['modifyTimeStart'] = timeStart.format('YYYY-MM-DD 00:00:00');
      reqParams['modifyTimeEnd'] = timeEnd.format('YYYY-MM-DD 23:59:59');
      delete reqParams['modifyTime'];
    }

    /**处理作废时间*/
    if (reqParams?.cancelTime) {
      const [timeStart, timeEnd] = reqParams.cancelTime;
      reqParams['cancelTimeStart'] = timeStart.format('YYYY-MM-DD 00:00:00');
      reqParams['cancelTimeEnd'] = timeEnd.format('YYYY-MM-DD 23:59:59');
      delete reqParams['cancelTime'];
    }

    delete reqParams['createFormNameDept'];
    delete reqParams['createFormNameUser'];
    if (reqParams?.createrID && reqParams?.createrID?.length > 0) {
      reqParams['createrID'] = reqParams?.createrID?.toString();
    } else {
      delete reqParams['createrID'];
    }

    delete reqParams['cancelFormNameDept'];
    delete reqParams['cancelFormNameUser'];
    if (reqParams?.cancelerID && reqParams?.cancelerID?.length > 0) {
      reqParams['cancelerID'] = reqParams?.cancelerID?.toString();
    } else {
      delete reqParams['cancelerID'];
    }

    run({ ...reqParams });
  }, []);

  /***页面数据及分页信息**/
  const tableValues = useMemo(() => {
    return {
      data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  /***搜索功能配置columns**/
  const searchList = useMemo(() => {
    return {
      assetType,
      searchList: ManageSearchColumns(assetType, areaListData),
    };
  }, [assetType, manageTableParams]);

  return {
    loading,
    reloadRequest,
    areaListData,
    manageTableParams,
    ...tableValues, //页面列表值
    ...searchList,
  };
};

export { MaterialManageServices };
